﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports System
Imports System.ComponentModel
Imports System.Collections.Generic

Imports System.Collections.ObjectModel


Imports System.Data

Imports System.IO
Imports System.Xml


Imports visRackLocations.clsDC_RackData


Public Class clsLocations


#Region " Properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_Location
    Public m_oc As Collection(Of dc_Location)



    Private m_deleteCommand As DbCommand
    Private m_insertCommand As DbCommand
    Private m_updateCommand As DbCommand

    Private strUserId As String

    Public Const readAll As String = "selecttblLocation"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblLocation


#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Location = New dc_Location(strUserId)

        Try

            Dim colLocKey As New System.Data.DataColumn
            colLocKey.ColumnName = "locKey"
            colLocKey.DataType = GetType(Integer)
            colLocKey.DefaultValue = m_dc.locKey
            tempTable.Columns.Add(colLocKey)

            Dim colLocClientKey As New System.Data.DataColumn
            colLocClientKey.ColumnName = "locClientKey"
            colLocClientKey.DataType = GetType(Integer)
            colLocClientKey.DefaultValue = m_dc.locClientKey
            tempTable.Columns.Add(colLocClientKey)

            Dim colLocClientName As New System.Data.DataColumn
            colLocClientName.ColumnName = "locClientName"
            colLocClientName.DataType = GetType(String)
            colLocClientName.DefaultValue = m_dc.locClientName
            tempTable.Columns.Add(colLocClientName)

            Dim colLocSiteId As New System.Data.DataColumn
            colLocSiteId.ColumnName = "locSiteId"
            colLocSiteId.DataType = GetType(String)
            colLocSiteId.DefaultValue = m_dc.locSiteId
            tempTable.Columns.Add(colLocSiteId)

            Dim colLocCompanyName As New System.Data.DataColumn
            colLocCompanyName.ColumnName = "locCompanyName"
            colLocCompanyName.DataType = GetType(String)
            colLocCompanyName.DefaultValue = m_dc.locCompanyName
            tempTable.Columns.Add(colLocCompanyName)

            Dim colLocAddress1 As New System.Data.DataColumn
            colLocAddress1.ColumnName = "locAddress1"
            colLocAddress1.DataType = GetType(String)
            colLocAddress1.DefaultValue = m_dc.locAddress1
            tempTable.Columns.Add(colLocAddress1)

            Dim colLocAddress2 As New System.Data.DataColumn
            colLocAddress2.ColumnName = "locAddress2"
            colLocAddress2.DataType = GetType(String)
            colLocAddress2.DefaultValue = m_dc.locAddress2
            tempTable.Columns.Add(colLocAddress2)

            Dim colLocCity As New System.Data.DataColumn
            colLocCity.ColumnName = "locCity"
            colLocCity.DataType = GetType(String)
            colLocCity.DefaultValue = m_dc.locCity
            tempTable.Columns.Add(colLocCity)

            Dim colLocState As New System.Data.DataColumn
            colLocState.ColumnName = "locState"
            colLocState.DataType = GetType(String)
            colLocState.DefaultValue = m_dc.locState
            tempTable.Columns.Add(colLocState)

            Dim colLocZip As New System.Data.DataColumn
            colLocZip.ColumnName = "locZip"
            colLocZip.DataType = GetType(Integer)
            colLocZip.DefaultValue = m_dc.locZip
            tempTable.Columns.Add(colLocZip)

            Dim colLocZipP4 As New System.Data.DataColumn
            colLocZipP4.ColumnName = "locZipP4"
            colLocZipP4.DataType = GetType(Integer)
            colLocZipP4.DefaultValue = m_dc.locZipP4
            tempTable.Columns.Add(colLocZipP4)

            Dim colLocCountry As New System.Data.DataColumn
            colLocCountry.ColumnName = "locCountry"
            colLocCountry.DataType = GetType(String)
            colLocCountry.DefaultValue = m_dc.locCountry
            tempTable.Columns.Add(colLocCountry)

            Dim colLocContact As New System.Data.DataColumn
            colLocContact.ColumnName = "locContact"
            colLocContact.DataType = GetType(String)
            colLocContact.DefaultValue = m_dc.locContact
            tempTable.Columns.Add(colLocContact)

            Dim colLocBackupContact As New System.Data.DataColumn
            colLocBackupContact.ColumnName = "locBackupContact"
            colLocBackupContact.DataType = GetType(String)
            colLocBackupContact.DefaultValue = m_dc.locBackupContact
            tempTable.Columns.Add(colLocBackupContact)

            Dim colLocPhone As New System.Data.DataColumn
            colLocPhone.ColumnName = "locPhone"
            colLocPhone.DataType = GetType(String)
            colLocPhone.DefaultValue = m_dc.locPhone
            tempTable.Columns.Add(colLocPhone)

            Dim colLocFax As New System.Data.DataColumn
            colLocFax.ColumnName = "locFax"
            colLocFax.DataType = GetType(String)
            colLocFax.DefaultValue = m_dc.locFax
            tempTable.Columns.Add(colLocFax)

            Dim colLocSubnet As New System.Data.DataColumn
            colLocSubnet.ColumnName = "locSubnet"
            colLocSubnet.DataType = GetType(String)
            colLocSubnet.DefaultValue = m_dc.locSubnet
            tempTable.Columns.Add(colLocSubnet)

            Dim colLocMask As New System.Data.DataColumn
            colLocMask.ColumnName = "locMask"
            colLocMask.DataType = GetType(String)
            colLocMask.DefaultValue = m_dc.locMask
            tempTable.Columns.Add(colLocMask)

            Dim colLocComments As New System.Data.DataColumn
            colLocComments.ColumnName = "locComments"
            colLocComments.DataType = GetType(String)
            colLocComments.DefaultValue = m_dc.locComments
            tempTable.Columns.Add(colLocComments)

            Dim colLocLastUpdate As New System.Data.DataColumn
            colLocLastUpdate.ColumnName = "locLastUpdate"
            colLocLastUpdate.DataType = GetType(Date)
            colLocLastUpdate.DefaultValue = m_dc.locLastUpdate
            tempTable.Columns.Add(colLocLastUpdate)

            Dim colLocLastEdit As New System.Data.DataColumn
            colLocLastEdit.ColumnName = "locLastEdit"
            colLocLastEdit.DataType = GetType(String)
            colLocLastEdit.DefaultValue = m_dc.locLastEdit
            tempTable.Columns.Add(colLocLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub

    '
    '
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_Location)

        Try


            With tempDC
                tempRow.SetField("locKey", .locKey)
                tempRow.SetField("locClientKey", .locClientKey)
                tempRow.SetField("locClientName", .locClientName)
                tempRow.SetField("locSiteId", .locSiteId)
                tempRow.SetField("locCompanyName", .locCompanyName)
                tempRow.SetField("locAddress1", .locAddress1)
                tempRow.SetField("locAddress2", .locAddress2)
                tempRow.SetField("locCity", .locCity)
                tempRow.SetField("locState", .locState)
                tempRow.SetField("locZip", .locZip)
                tempRow.SetField("locZipP4", .locZipP4)
                tempRow.SetField("locContact", .locContact)
                tempRow.SetField("locBackupContact", .locBackupContact)
                tempRow.SetField("locPhone", .locPhone)
                tempRow.SetField("locFax", .locFax)
                tempRow.SetField("locSubnet", .locSubnet)
                tempRow.SetField("locMask", .locMask)
                tempRow.SetField("locComments", .locComments)
                tempRow.SetField("locLastUpdate", .locLastUpdate)
                tempRow.SetField("locLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .locKey = tempRow.Item("locKey")
                .locClientKey = tempRow.Item("locClientKey")
                .locClientName = tempRow.Item("locClientName")
                .locSiteId = tempRow.Item("locSiteId")
                .locCompanyName = tempRow.Item("locCompanyName")
                .locAddress1 = tempRow.Item("locAddress1")
                .locAddress2 = tempRow.Item("locAddress2")
                .locCity = tempRow.Item("locCity")
                .locState = tempRow.Item("locState")
                .locZip = tempRow.Item("locZip")
                .locZipP4 = tempRow.Item("locZipP4")
                .locContact = tempRow.Item("locContact")
                .locBackupContact = tempRow.Item("locBackupContact")
                .locPhone = tempRow.Item("locPhone")
                .locFax = tempRow.Item("locFax")
                .locSubnet = tempRow.Item("locSubnet")
                .locMask = tempRow.Item("locMask")
                .locComments = tempRow.Item("locComments")
                .locLastUpdate = tempRow.Item("locLastUpdate")
                .locLastEdit = tempRow.Item("locLastEdit")
            End With

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_Location)

        Try

            With tempDC
                .initProperties()
                .locKey = tempRow.Item("locKey")
                .locClientKey = tempRow.Item("locClientKey")
                .locClientName = tempRow.Item("locClientName")
                .locSiteId = tempRow.Item("locSiteId")
                .locCompanyName = tempRow.Item("locCompanyName")
                .locAddress1 = tempRow.Item("locAddress1")
                .locAddress2 = tempRow.Item("locAddress2")
                .locCity = tempRow.Item("locCity")
                .locState = tempRow.Item("locState")
                .locZip = tempRow.Item("locZip")
                .locZipP4 = tempRow.Item("locZipP4")
                .locContact = tempRow.Item("locContact")
                .locBackupContact = tempRow.Item("locBackupContact")
                .locFax = tempRow.Item("locFax")
                .locPhone = tempRow.Item("locPhone")
                .locSubnet = tempRow.Item("locSubnet")
                .locMask = tempRow.Item("locMask")
                .locComments = tempRow.Item("locComments")
                .locLastUpdate = tempRow.Item("locLastUpdate")
                .locLastEdit = tempRow.Item("locLastEdit")
            End With

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Location)
                Else
                    m_oc = New Collection(Of dc_Location)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Location
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Location
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function


#End Region

#Region " standard calls "

    '
    '
    '
    Private Sub buildInsertLocationCommand()


        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertLocation")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand
            m_db.AddInParameter(cmdActive, "locClientKey", DbType.Int32, "locClientKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locClientName", DbType.String, "locClientName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locSiteId", DbType.String, "locSiteId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCompanyName", DbType.String, "locCompanyName", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locAddress1", DbType.String, "locAddress1", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locAddress2", DbType.String, "locAddress2", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCity", DbType.String, "locCity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locState", DbType.String, "locState", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locZip", DbType.Int32, "locZip", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locZipP4", DbType.Int32, "locZipP4", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCountry", DbType.String, "locCountry", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locContact", DbType.String, "locContact", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locBackupContact", DbType.String, "locBackupContact", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locPhone", DbType.String, "locPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locFax", DbType.String, "locFax", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locSubnet", DbType.String, "locSubnet", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locMask", DbType.String, "locMask", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locComments", DbType.String, "locComments", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locLastUpdate", DbType.DateTime, "locLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locLastEdit", DbType.String, "locLastEdit", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildUpdateLocationCommand()


        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateLocation")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand
            m_db.AddInParameter(cmdActive, "locKey", DbType.Int32, "locKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locClientKey", DbType.Int32, "locClientKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locClientName", DbType.String, "locClientName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locSiteId", DbType.String, "locSiteId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCompanyName", DbType.String, "locCompanyName", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locAddress1", DbType.String, "locAddress1", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locAddress2", DbType.String, "locAddress2", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCity", DbType.String, "locCity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locState", DbType.String, "locState", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locZip", DbType.Int32, "locZip", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locZipP4", DbType.Int32, "locZipP4", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locCountry", DbType.String, "locCountry", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locContact", DbType.String, "locContact", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locBackupContact", DbType.String, "locBackupContact", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locPhone", DbType.String, "locPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locFax", DbType.String, "locFax", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locSubnet", DbType.String, "locSubnet", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locMask", DbType.String, "locMask", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locComments", DbType.String, "locComments", DataRowVersion.Current)

            m_db.AddInParameter(cmdActive, "locLastUpdate", DbType.DateTime, "locLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locLastEdit", DbType.String, "locLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteLocationCommand()



        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteLocation")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand
            m_db.AddInParameter(cmdActive, "locKey", DbType.Int32, "locKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "locClientKey", DbType.Int32, "locClientKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub


    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblLocation.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addLocationDataSourceRecord _
            (ByVal tempDC As dc_Location, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblLocation.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblLocation.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Location)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Location)
            End If

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_Location(strUserId)
            Else
                m_dc = New dc_Location(strUserId)
            End If

            ' initialize our new table with column names
            m_dt.TableName = dc_racktables.tblLocation.tostring
            CreateTableColumns(m_dt)
            m_ds.Tables.Add(m_dt)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


#End Region

#Region " specific database calls "

    '
    '
    '
    Public Sub dbSelectAllLocations()


        Try

            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblLocation.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblLocation.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub dbSelectAllClientLocations(ByVal intClient As Int32)


        Try



            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectLocationByClientKey")
            m_db.AddInParameter(cmd, "@clientKey", DbType.Int32, intClient)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblLocation.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblLocation.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectLocationBySiteKey(ByVal intSite As Int32)


        Try



            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectLocationBySiteKey")
            m_db.AddInParameter(cmd, "@siteKey", DbType.Int32, intSite)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblLocation.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblLocation.tostring)

            ' if we find the record move it to the properties
            If m_dt.Rows.Count = 1 Then
                Dim m_dr As DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

#End Region

#Region " constructor "

    Public Sub New()

        Try

            m_db = DatabaseFactory.CreateDatabase()
            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '
            If m_dc Is Nothing Then
                m_dc = New dc_Location(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Location(strUserId)
            End If

            initDataSetAndTable()



            buildDeleteLocationCommand()
            buildInsertLocationCommand()
            buildUpdateLocationCommand()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

#End Region



End Class
